package com.acheli.registry.entity.render;

import com.acheli.registry.entity.model.CustomGeckolibModel;
import com.acheli.rideable.entites.ComponentEntity;
import com.acheli.rideable.entites.RideableEntity;
import com.acheli.rideable.functionLathes.ComponentFL;
import com.acheli.utils.rotate.RotateWith;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import org.jetbrains.annotations.NotNull;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeObject;
import software.bernie.geckolib.renderer.GeoEntityRenderer;

public class ComponentEntityRender extends GeoEntityRenderer<ComponentEntity> {
    private EntityRendererProvider.Context context;
    public ComponentEntityRender(EntityRendererProvider.Context context) {
        super(context, new CustomGeckolibModel<>());
        this.context = context;
    }

    @Override
    public boolean shouldRender(ComponentEntity entity, Frustum camera, double camX, double camY, double camZ) {
        return super.shouldRender(entity, camera, camX, camY, camZ) || entity.isEmpty || entity.factory.getProduct().shouldRender();
    }

    @Override
    public void render(ComponentEntity entity, float yaw, float partialTick,
                       @NotNull PoseStack poseStack, @NotNull MultiBufferSource buffer, int packedLight) {
        CustomGeckolibModel<ComponentEntity> translatedModel = ((CustomGeckolibModel<ComponentEntity>) model);

        if (entity.factory != null) {

            try {
                NativeObject js =
                        entity.factory.getProductFunction().getFL().getJS();
                translatedModel.setUp(
                        Context.toString(js.get("model")),
                        Context.toString(js.get("texture")),
                        Context.toString(js.get("animation"))
                );

                if (! translatedModel.shouldCancel()) {
                    ComponentFL.Orientation orientation = entity.getComponentFL().orientation;
                    poseStack.mulPose(Axis.YP.rotationDegrees(ComponentFL.getOrientAngle(orientation)));
//                    poseStack.mulPose(RotateWith.translateRotation(entity.factory.getProduct().getRotation()));

                    if (entity.getVehicle() instanceof RideableEntity rideable) {
                        rideable.doFactory(factoryNotNull -> {
                            poseStack.mulPose(
                                    RotateWith.translateRotation(
                                            factoryNotNull.getProductFunction().getFL().totalRotation
                                    )
                            );
                        });
                    }
                    entity.getComponentFL().applyRotation(poseStack);
                    super.render(entity,yaw,partialTick,poseStack,buffer,packedLight);
                }
            } catch (Exception e) {
                poseStack.popPose();
            }


            if (entity.factory.getProductFunction() != null && entity.factory.getProductFunction().getFL() != null) {
                entity.factory.getProductFunction().render(context, entity, yaw, partialTick, poseStack, buffer, packedLight);
                entity.factory.getProductFunction().renderTick(poseStack, partialTick);
                entity.factory.getProductFunction().getFL().renderTick(entity, poseStack, partialTick);

            }
        }
        entity.applyRotation(poseStack);

    }

}